<?php

namespace app\store\controller\game;

use app\store\controller\Controller;
use app\store\model\activity\LuckyBag as LuckyBagModel;
use app\store\service\game\Game as GameService;
use cores\exception\BaseException;
use think\App;
use think\db\exception\DbException;
use think\facade\Cache;
use think\response\Json;


class Game extends Controller
{

    private $gameService;
    public function __construct(App $app,GameService $gameService)
    {
        parent::__construct($app);
        $this->gameService = $gameService;
    }

    /**游戏列表
     * @return Json
     * @throws DbException
     */
    public function index(): Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer',
            ]
        );
        $list = $this->gameService->getList($params);
        return $this->renderSuccess(['list' => $list]);
    }

    /**游戏详情
     * @return Json
     * @throws BaseException
     */
    public function detail(): Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'id' => 'integer|require'
            ]
        );
        $id = $params['id'];
        $res = $this->gameService->detail($id);
        return $this->renderSuccess($res);

    }

    /**
     * 创建游戏
     * @return Json
     */
    public function save(): Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'room_id' => 'require',
                'cover_url' => 'require',
                'desc' => 'require',
                'category_id|分类ID' => 'require',
                'sort' => 'require',
                'remark|备注' => 'require',
                'id' => 'integer'
            ]
        );
        $res = $this->gameService->save($params);
        if(!$res){
            return $this->renderError('保存游戏失败');
        }
        return $this->renderSuccess([],'保存成功');
    }




    /**
     * 删除游戏
     * @return Json
     */
    public function delete(): Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'id' => 'require'
            ]
        );
        $res = $this->gameService->delete($params['id']);
        if(!$res){
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

    /**
     * @return Json
     */
    public function log():Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer'
            ]
        );
        $res = $this->gameService->getLog($params);
        if(!$res){
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }


}
